package com.bridgeintelligent.tag.webserver.workflow.repository;

import com.bridgeintelligent.tag.webserver.workflow.Instance;
import com.github.pagehelper.Page;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface InstanceMapper {
    void save(Instance instance);

    void update(Instance instance);

    Instance findOne(@Param("id") String id);

    @Getter
    @Setter
    @Accessors(chain = true)
    class CheckByResourceParam {
        private String resource;
        private String workflowId;
        private int currentStatus;
        private boolean containsDeleted;
        private String instanceId;
    }

    List<Object> checkByResource(CheckByResourceParam param);

    @Getter
    @Setter
    @Accessors(chain = true)
    class FindByResourcesParam {
        private List<String> resourceIds;
        private boolean containsDeleted;
        private int currentStatus;

        public FindByResourcesParam addResourceId(String resourceId) {
            if (resourceIds == null) {
                resourceIds = new ArrayList<>();
            }
            resourceIds.add(resourceId);
            return this;
        }
    }

    @MapKey("resource")
    Map<String, Instance> findByResources(FindByResourcesParam param);

    @Getter
    @Setter
    @Accessors(chain = true)
    class AllForSubmitter {
        private String owner;
        private List<String> workflowIds;
        private boolean containsDeleted;
        private int pageNum;
        private int pageSize;
    }

    Page<Instance> findAllForSubmitter(AllForSubmitter param);

    @Data
    @Accessors(chain = true)
    class AllForApprover {
        private String userId;
        private String workflowId;
        private List<String> approverCodes;
        private boolean containsDeleted;
        private int pageNum;
        private int pageSize;

        public AllForApprover addApproverCode(String code) {
            if (approverCodes == null) {
                approverCodes = new ArrayList<>();
            }
            approverCodes.add(code);
            return this;
        }
    }

    Page<Instance> findAllForApprover(AllForApprover param);

    /**
     * 导出客群下载后删除审批流
     * @param map
     */
    void delByWorkIdResourceId(Map<String, String> map);

    Instance findInstance(Map<String, String> map);
}
